package com.webcamtracker.ui.grid;

import com.webcamtracker.configuration.RootPref;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class UIGridPropertiesPanel extends JPanel {


    public UIGridPropertiesPanel() {
        super(true);

        setupUI();
    }


    public void setupUI() {

        this.setLayout(new GridLayout(1, 1));

        this.add(setupGrid());

    }


    private JPanel setupGrid() {
        JPanel gridAnalizer = new JPanel();
        CompoundBorder gridBorder = BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Grid"),
                BorderFactory.createEmptyBorder(5, 5, 5, 5));
        JSpinner weightSpiner = new JSpinner(new SpinnerNumberModel(4, 1, 20, 1));
        weightSpiner.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSpinner spiner = (JSpinner) e.getSource();
                Integer value = (Integer) spiner.getModel().getValue();
                RootPref.getGridPref().putInt(RootPref.KEY_GRID_WIDTH, value);
            }
        });
        gridAnalizer.add(new JLabel("Width"));
        gridAnalizer.add(weightSpiner);
        JSpinner heightSpiner = new JSpinner(new SpinnerNumberModel(4, 1, 20, 1));
        heightSpiner.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSpinner spiner = (JSpinner) e.getSource();
                Integer value = (Integer) spiner.getModel().getValue();
                RootPref.getGridPref().putInt(RootPref.KEY_GRID_HEIGHT, value);
            }
        });
        gridAnalizer.add(new JLabel("Height"));
        gridAnalizer.add(heightSpiner);

        JSpinner countPixelSpiner = new JSpinner(new SpinnerNumberModel(RootPref.getGridPref().getInt(RootPref.KEY_GRID_ACCEPTANCE,RootPref.DEF_VALUE_GRID_ACCEPTANCE), 1, 20, 1));
        countPixelSpiner.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSpinner spiner = (JSpinner) e.getSource();
                Integer value = (Integer) spiner.getModel().getValue();
                RootPref.getGridPref().putInt(RootPref.KEY_GRID_ACCEPTANCE, value);
            }
        });
        gridAnalizer.add(new JLabel("Accep. Crit."));
        gridAnalizer.add(countPixelSpiner);

        gridAnalizer.setBorder(gridBorder);
        return gridAnalizer;
    }


}
